/*
AlliedBridge Content Management System
Copyright (C) 2006 AlliedBridge

This program is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software Foundation;

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.volume4.alliedbridge.model.media;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.io.*;
import java.util.*;
import java.lang.*;
import org.apache.commons.fileupload.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author  Design_DEMON
 * @version 1.0
 * Based on base classes from org.apache.commons.fileupload
 */
public class UploadManager extends HttpServlet {
    public String folder = "/Program Files/Apache Group/Tomcat 4.1/webapps/magforcetrading/cataloque/";
    public String tempFolder = "/temp";    
    //public String folder = "/var/www/vhosts/Magforce/webapps/Magforce/cataloque/";    
    //public String tempFolder = "/var/www/vhosts/Magforce/webapps/Magforce/temp/";    
    public String DRIVER, URL, USER, PASS, fieldName, fieldData, fileName, fileFieldName;
    public String prodName, prodDetail, prodImgURI, prodImgPreview; 
        
    public void init() throws ServletException {
    ServletContext context = getServletContext();
    DRIVER = context.getInitParameter("driver");
    URL = context.getInitParameter("dburl");
    USER = context.getInitParameter("dbuser");
    PASS = context.getInitParameter("dbpass");
    }
    
    protected void processRequest(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, java.io.IOException {        
        
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        Connection con = null;
        
        try {            
            
            DiskFileUpload upload = new DiskFileUpload();
            
            upload.setSizeMax(400000000);
            upload.setSizeThreshold(4096);
            upload.setRepositoryPath(tempFolder);
            
            List items = upload.parseRequest(req);
            
            Iterator iter = items.iterator();
            while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();            
            
            if (item.isFormField()) {          
                               
            } else if(item.getName().equals("")) {
              
                } else {
            fileName = item.getName();            
            fileFieldName = item.getFieldName();              
            
            StringTokenizer tokenizer = new StringTokenizer(fileName, "\\, :, /");            
            int amount = tokenizer.countTokens();             
            for (int i = 0; i < amount -1; i++) {
                tokenizer.nextToken();        
            }               
            String currentFile = tokenizer.nextToken();            
            
            File diskFile = new File(folder, currentFile);
                        
            item.write(diskFile);           
            
            item.delete();            
                }     
        
    }

res.sendRedirect("../../result/product_added.jsp");
            
        }   catch(FileUploadException fue) {         
         fue.printStackTrace();
         out.println("There was and error when reading and writing the file to the server.");
               
        } catch(Exception e) {
            e.printStackTrace();
        }
}
    
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, java.io.IOException {
        processRequest(req, res);
    }    
    
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, java.io.IOException {
        processRequest(req, res);
    }
}
